// invoice.js  发票详情
var shihan = require('../../common/fetch')
var config = require('../../common/config')
const app = getApp();
var winScale = 750 / app.data.deviceInfo.windowWidth
var invoiceinfo
var range;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoicetitle: ['个人', '单位'],
    index:0,
    typeindex: 0,
    currentindex: 0,
    elec_person: false,
    elec_unit: true,
    paper_person: true,
    paper_unit: true,
    selectflag: false,
    paper_person_common: true,
    paper_person_special: true,
    bupiaoflag: false
  },
  invoicetitlechange: function (e) {//发票抬头
    this.setData({
      currentindex: e.target.dataset.index,
    })
    this.invoiceinfochange()
  },
  listenerPickerSelected: function (e) {//发票类型
    if (this.data.selectflag) {
      this.setData({
        selectflag: false
      });
    } else {
      this.setData({
        selectflag: true
      });
    }
    this.setData({
      index: e.detail.value
    });
    this.invoiceinfochange()
  },
  selectArrow: function () {
    if (this.data.selectflag) {
      this.setData({
        selectflag: false
      });
    } else {
      this.setData({
        selectflag: true
      });
    }
  },
  cancelSelect: function () {
    this.setData({
      selectflag: false
    });
  },
  listenerPickerSelectedtype: function (e) {//增值税
    if (this.data.selectflag) {
      this.setData({
        selectflag: false
      });
    } else {
      this.setData({
        selectflag: true
      });
    }
    this.setData({
      typeindex: e.detail.value,
    });
    this.invoiceinfochange()
  },
  invoiceinfochange:function(ev){
    var index = this.data.index
    var typeindex = this.data.typeindex
    var currentindex = this.data.currentindex
    var storeid = this.data.storeid
    if (this.data.array[index] == '电子发票' && currentindex == 0) {
      this.setData({
        elec_person: false,
        elec_unit: true,
        paper_person: true,
        paper_unit: true,
        paper_person_common: true,
        paper_person_special: true,
      })

      if(ev){
        if (ev.detail.value.peoname == '') {
          shihan.showToast.call(this, '请输入姓名');
        } else if (ev.detail.value.peoplemail == '') {
          shihan.showToast.call(this, '请输入邮箱');
        } else {
          invoiceinfo[storeid] = {
            "type": "1",//发票类型：0不开，1电子，2纸质
            "name_type": "1",//发票抬头类型：0不开，1个人，2单位  
            "name": ev.detail.value.peoname,//发票抬头
            "email": ev.detail.value.peoplemail//邮箱
          }
          this.setData({
            invoiceinfo: invoiceinfo
          })
          wx.setStorage({
            key: "invoicekey",
            data: this.data.invoiceinfo,
            success: function () { 
              wx.navigateBack()
            }
          });
        }
      }
    } else if (this.data.array[index] == '电子发票' && currentindex == 1) {
      this.setData({
        elec_person: true,
        elec_unit: false,
        paper_person: true,
        paper_unit: true,
        paper_person_common: true,
        paper_person_special: true,
      })
      if(ev){
        if (ev.detail.value.unitname == '') {
          shihan.showToast.call(this, '请输入单位名称');
        } else if (ev.detail.value.unitnumber == '') {
          shihan.showToast.call(this, '请输入纳税人识别号');
        } else if (ev.detail.value.unitemail == '') {
          shihan.showToast.call(this, '请输入邮箱');
        } else {
          invoiceinfo[storeid] = {
            "type": "1",//发票类型：0不开，1电子，2纸质
            "name_type": "2",//发票抬头类型：0不开，1个人，2单位  
            "name": ev.detail.value.unitname,//发票抬头
            "duty_paragraph": ev.detail.value.unitnumber,//纳税人识别号
            "email": ev.detail.value.unitemail//邮箱
          }
          this.setData({
            invoiceinfo: invoiceinfo
          })
          wx.setStorage({
            key: 'invoicekey',
            data: this.data.invoiceinfo,
            success: function () {
              wx.navigateBack()
            }
          })
        }
      }
    } else if (this.data.array[index] == '纸质发票' && currentindex == 0) {
      this.setData({
        elec_person: true,
        elec_unit: true,
        paper_person: false,
        paper_unit: true,
        paper_person_common: true,
        paper_person_special: true,
      })
      if (ev) {
        if (ev.detail.value.papername == '') {
          shihan.showToast.call(this, '请输入姓名');
        } else {
          invoiceinfo[storeid] = {
            "type": "2",//发票类型：0不开，1电子，2纸质
            "name_type": "1",//发票抬头类型：0不开，1个人，2单位  
            "name": ev.detail.value.papername//发票抬头
          }
          this.setData({
            invoiceinfo: invoiceinfo
          })
          wx.setStorage({
            key: 'invoicekey',
            data: this.data.invoiceinfo,
            success: function () {
              wx.navigateBack()
            }
          })
        }
      }
    } else if (this.data.array[index] == '纸质发票' && currentindex == 1) {
      if (this.data.arraytype[typeindex] == '增值税普通发票'){
        this.setData({
          elec_person: true,
          elec_unit: true,
          paper_person: true,
          paper_unit: false,
          paper_person_common: false,
          paper_person_special: true,
        })
        if(ev){
          if (ev.detail.value.commonname == '') {
            shihan.showToast.call(this, '请输入单位名称');
          } else if (ev.detail.value.commonnumber == '') {
            shihan.showToast.call(this, '请输入纳税人识别号');
          } else {
            invoiceinfo[storeid] = { 
              "type": "2",//发票类型：0不开，1电子，2纸质
              "name_type": "2",//发票抬头类型：0不开，1个人，2单位  
              "vat_type": "2",//增值税发票类型1专用2普通
              "name": ev.detail.value.commonname,//发票抬头
              "duty_paragraph": ev.detail.value.commonnumber,//纳税人识别号
              "address": ev.detail.value.commonaddress,//注册地址
              "tel": ev.detail.value.commonphone,//电话
              "bank_name": ev.detail.value.commonbank,//银行名称
              "bank_number": ev.detail.value.commonbanknum//银行账户
            }
            this.setData({
              invoiceinfo: invoiceinfo
            })
            wx.setStorage({
              key: 'invoicekey',
              data: this.data.invoiceinfo,
              success: function () {
                wx.navigateBack()
              }
            })
          }
        }
      } else if (this.data.arraytype[typeindex] == '增值税专用发票'){
        this.setData({
          elec_person: true,
          elec_unit: true,
          paper_person: true,
          paper_unit: false,
          paper_person_common: true,
          paper_person_special: false,
        })
        if (ev) {
          if (ev.detail.value.specialname == '') {
            shihan.showToast.call(this, '请输入单位名称');

          } else if (ev.detail.value.specialnumber == '') {
            shihan.showToast.call(this, '请输入纳税人识别号');
            
          } else if (ev.detail.value.specialaddress == '') {
            shihan.showToast.call(this, '请输入注册地址');

          } else if (ev.detail.value.specialphone == '') {
            shihan.showToast.call(this, '请输入联系电话');

          } else if (ev.detail.value.specialbank == '') {
            shihan.showToast.call(this, '请输入开户银行');

          } else if (ev.detail.value.specialbanknum == '') {
            shihan.showToast.call(this, '请输入银行账号');

          }else {
            invoiceinfo[storeid] = {
              "type": "2",//发票类型：0不开，1电子，2纸质
              "name_type": "2",//发票抬头类型：0不开，1个人，2单位
              "vat_type": "1",//增值税发票类型1专用2普通
              "name": ev.detail.value.specialname,//发票抬头
              "duty_paragraph": ev.detail.value.specialnumber,//纳税人识别号
              "address": ev.detail.value.specialaddress,//注册地址
              "tel": ev.detail.value.specialphone,//电话
              "bank_name": ev.detail.value.specialbank,//银行名称
              "bank_number": ev.detail.value.specialbanknum//银行账户
            }
            this.setData({
              invoiceinfo: invoiceinfo
            })
            wx.setStorage({
              key: 'invoicekey',
              data: this.data.invoiceinfo,
              success: function () {
                wx.navigateBack()
              }
            })
          }
        }
      }
    }
    // console.log(this.data)
  },
  formSubmit: function (ev) {
    this.invoiceinfochange(ev)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    range = 794 / winScale;
    var animation = wx.createAnimation({
      duration: 350,
      timingFunction: 'ease-in-out',
    });
    this.animation = animation;
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      salesruleid: options.salesruleid
    })
    invoiceinfo = wx.getStorageSync('invoicekey') || {};
    this.setData({
      invoicetype: JSON.parse(options.type),
      invoiceinfo: invoiceinfo,
      storeid: Number(options.storeid),
      invoicenote: options.invoicenote
    })
    // console.log(this.data)
    // invoicetitle: ['个人', '单位'],
    // array: ['电子发票', '纸质发票'],
    // arraytype: ['增值税普通发票', '增值税专用发票'],
    if (this.data.invoicetype[0] == 0){
      this.setData({
        array: ['电子发票', '纸质发票']
      })
    } else if (this.data.invoicetype[0] == 1){
      this.setData({
        array: ['电子发票']
      })
    } else if (this.data.invoicetype[0] == 2) {
      this.setData({
        array: ['纸质发票']
      })
    }
    if (this.data.invoicetype[1] == 0) {
      this.setData({
        arraytype: ['增值税普通发票', '增值税专用发票']
      })
    } else if (this.data.invoicetype[1] == 1) {
      this.setData({
        arraytype: ['增值税专用发票']
      })
    } else if (this.data.invoicetype[1] == 2) {
      this.setData({
        arraytype: ['增值税普通发票']
      })
    }
    this.invoiceinfochange()
  },
  setModalStatus: function (e) {
    if (e.currentTarget.dataset.status == 0) {
      this.animation.translateY(range).step();
      this.setData({
        showModalStatus: false,
        animationData: this.animation.export()
      });
    } else {
      this.animation.translateY(-range).step();
      this.setData({
        showModalStatus: true,
        animationData: this.animation.export()
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})